package com.gomo;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Vector;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class GlobalService {
	private static SharedPreferences appSettings;
	private static String userId = "";
	private static String userEmail = "";
	private static String userPinCode = "";

	public static String getUserId() {
		return userId;
		// return "17";
	}

	public static void setUserId(String userid) {
		GlobalService.userId = userid;
	}

	public static String getUserEmail() {
		return userEmail;
	}

	public static void setUserEmail(String userpin) {
		userPinCode = userpin;
	}

	public static String getUserPinCode() {
		return userPinCode;
	}

	public static void setUserPicCode(String userpin) {
		userPinCode = userpin;
	}

	public static void HandleException(Exception e) {
		Log.e("Class name is ", e.getStackTrace()[1].getClassName());
		Log.e("Method name is ", e.getStackTrace()[1].getMethodName());
		// e.printStackTrace();
	}

	/*
	 * public static void setPref(String key, String value) { //static
	 * SharedPreferences.Editor prefEditor = appSettings.edit();
	 * //prefEditor.putString(key, value); //prefEditor.commit(); //
	 * SharedPreferences appSettings; // return ""; }
	 * 
	 * public static String getPref(String key, String value) { //
	 * SharedPreferences appSettings; return ""; }
	 */
	public static boolean isOnline(Context cxt) {
		ConnectivityManager connectivityManager = (ConnectivityManager) cxt
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetworkInfo = connectivityManager
				.getActiveNetworkInfo();
		return activeNetworkInfo != null;

	}

	public static boolean isHostAvailable(String hostUrl) {
		Socket socket = null;
		boolean reachable = false;
		try {
			try {
				socket = new Socket(hostUrl, 80);
				reachable = true;
			} catch (UnknownHostException e) {
				GlobalService.HandleException(e);
				// reachable = false;
			} catch (IOException e) {
				GlobalService.HandleException(e);
				// reachable = false;
			}
		} finally {
			if (socket != null)
				try {
					socket.close();
				} catch (IOException e) {
					GlobalService.HandleException(e);
				}
		}
		return reachable;
	}

	public static boolean isHostAvailable1(String hostUrl) {
		try {
			InetAddress.getByName(hostUrl).isReachable(5000);
			// InetAddress.getByAddress(hostUrl, null).isReachable(5000);
			return true;
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	public static String[] splitString(String original, String separator) {
		Vector nodes = new Vector();
		int index = original.indexOf(separator);
		while (index >= 0) {
			nodes.addElement(original.substring(0, index));
			original = original.substring(index + separator.length());
			index = original.indexOf(separator);
		}
		nodes.addElement(original);
		String[] result = new String[nodes.size()];
		if (nodes.size() > 0) {
			for (int loop = 0; loop < nodes.size(); loop++) {
				result[loop] = (String) nodes.elementAt(loop);
			}
		}
		return result;
	}

	public static void MessageBox(String message, Context ctx) {
		AlertDialog.Builder alertbox = new AlertDialog.Builder(ctx);
		alertbox.setMessage(message);

		alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
			}
		});
		alertbox.show();
	}

}
